package com.longshine.arthmetic;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 * 可显示结点类
 *  
 *
 * @classname   NodeDrawable.java 
 * @author      YitianC
 * @version     Revision: 1.1,  Date: 2008/05/14 01:43:47  
 * @history     
 *              Mar 13, 2012 4:54:55 PM  
 *
 */
public class NodeDrawable extends Comparabel implements Drawable {

	protected int radius = 30;//直径   
	protected Color rgb = Color.RED;   	//结点背景颜色
	protected int x = 100;//圆心位置
	protected int y = 100;
	JLabel jl = new JLabel(this.weight+""); 		//结点中用于显示权重的标签
	public void draw(JFrame container) {	
		beforeDraw();
		//画圆
		Graphics g = container.getContentPane().getGraphics();
		g.setColor(rgb);
		g.fillOval(x-radius/2, y-radius/2, radius, radius);
		
		if(!hasComponent(container,jl)){
			jl.setBounds(x-8, y-8, 15, 15);
			jl.setOpaque(true);
			jl.setBackground(rgb);
			jl.setForeground(Color.WHITE);
			jl.setText(this.weight+"");
			container.add(jl);
			
		}
		//标签重新绘制
		jl.repaint();
	}
	/**
	 * 
	 * 查看container中是否存在组件c 
	 * 
	 * @param container
	 * @param c
	 * @return
	 * @author      YitianC 
	 * @history 
	 *              YitianC Mar 13, 2012 5:01:18 PM
	 */
	private boolean hasComponent(JFrame container,Component c){
		for(Component c1:container.getComponents()){
			if(c1== c){
				return true;
			}
		}
		return false;
	}
	public int getRadius() {
		return radius;
	}
	public void setRadius(int radius) {
		this.radius = radius;
	}
	public Color getRgb() {
		return rgb;
	}
	public void setRgb(Color rgb) {
		this.rgb = rgb;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public void beforeDraw() {
		this.rgb = Color.BLACK;
		
	}

}
